package com.kedacom.freedomLabel.base;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.adapter.SettingItemAdapter;
import com.kedacom.freedomLabel.databinding.BaseSettingFunctionBinding;
import com.kedacom.freedomLabel.model.bean.SettingItem;
import com.kedacom.freedomLabel.utils.NumFormatUtil;
import com.kedacom.freedomLabel.utils.QRCodeUtil;
import com.kedacom.lego.util.AndroidUtils;
import com.seray.scaleviewlib.dialog.ScaleKeyDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置页面基础类
 *
 * @author SR_Android
 */
public abstract class BaseSettingActivity extends BaseActivity<BaseSettingFunctionBinding, BaseViewModel> {

    protected static final String KEY;

    // 生成规则：（电子秤系统时间）月日的前一天 + 37
    static {
        KEY = NumFormatUtil.getRandomPassword(NumFormatUtil.PASSWORD_TO_SETTING);
    }

    protected List<SettingItem> mData = new ArrayList<>();
    protected SettingItemAdapter mAdapter;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                setResult(RESULT_OK, getResultIntent());
                finish();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
        initAdapter();
        setItemClickListener();
    }

    protected abstract Intent getResultIntent();

    protected abstract void setBarTitle();

    protected abstract void setItemClickListener();
    protected abstract void initData();

    protected void initView() {
        mBinding.functionRecycler.setLayoutManager(new GridLayoutManager(this, 2));
        setBarTitle();
    }

    protected void initAdapter() {
        mAdapter = new SettingItemAdapter(mData);
        mBinding.functionRecycler.setAdapter(mAdapter);
    }

    protected void displayMessageView(String message, String url) {
        findViewById(R.id.base_setting_message_layout).setVisibility(View.VISIBLE);
        TextView view = findViewById(R.id.base_setting_message);
        view.setText(message);
        ImageView imageView = findViewById(R.id.base_setting_qr);
        Bitmap bitmap = QRCodeUtil.createQRCodeBitmap(url,
                AndroidUtils.dp2px(this, 150),
                AndroidUtils.dp2px(this, 150));
        imageView.setImageBitmap(bitmap);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mData.clear();
        mData = null;
    }

    protected void openKeyInputDialog(final int from, @Nullable final OnKeyInputCompleteListener listener) {
        ScaleKeyDialog keyDialog = new ScaleKeyDialog(BaseSettingActivity.this);
        keyDialog.show();
        keyDialog.setOnPositiveClickListener((scaleKeyDialog, s) -> {
            if (listener != null)
                listener.onKeyInputComplete(from, s);
            scaleKeyDialog.dismiss();
        });
    }

    public interface OnKeyInputCompleteListener {
        void onKeyInputComplete(int from, String key);
    }
}